package migrate

import (
	"sale-admin/config/mysql"
	"sale-admin/internal/models"
)

// 商品分类表改名
func (MigrateFunc) Script3() (err error) {
	mysql.NewDB().Migrator().RenameTable("ma_goods", "ma_goods_class")
	return nil
}

// 商户配置表改名
func (MigrateFunc) Script4() (err error) {
	mysql.NewDB().Migrator().RenameTable("ma_user_shop_info", "ma_merchant_config")
	return nil
}

func (MigrateFunc) Script5() (err error) {
	mysql.NewDB().Migrator().AddColumn(&models.MaRedemptionCode{}, "batch_id")
	mysql.NewDB().Migrator().AddColumn(&models.MaRedemptionLog{}, "batch_id")
	return nil
}

func (MigrateFunc) Script6() (err error) {
	mysql.NewDB().Migrator().AddColumn(&models.MaAIServiceFaceCount{}, "merchant_id")
	mysql.NewDB().Migrator().AddColumn(&models.MaGoodsClass{}, "merchant_id")
	mysql.NewDB().Migrator().AddColumn(&models.MaGoodsClick{}, "merchant_id")
	mysql.NewDB().Migrator().AddColumn(&models.MaGoodsInfo{}, "merchant_id")
	mysql.NewDB().Migrator().AddColumn(&models.MaRedemptionCode{}, "merchant_id")
	mysql.NewDB().Exec("update ma_ai_service_face_count set merchant_id = id")
	mysql.NewDB().Exec("update ma_goods_class set merchant_id = id")
	mysql.NewDB().Exec("update ma_goods_click set merchant_id = id")
	mysql.NewDB().Exec("update ma_goods_info set merchant_id = id")
	mysql.NewDB().Exec("update ma_redemption_code set merchant_id = id")
	return nil
}

func (MigrateFunc) Script7() (err error) {
	mysql.NewDB().Migrator().AddColumn(&models.MaAIServiceFaceCount{}, "areas3")
	mysql.NewDB().Exec("UPDATE ma_ai_service_face_count a LEFT JOIN ma_robot r ON r.robot_name = a.robot_name SET a.areas3 = r.areas3")
	return nil
}

func (MigrateFunc) Script8() (err error) {
	mysql.NewDB().Migrator().AddColumn(&models.MaOrder{}, "merchant_id")
	mysql.NewDB().Migrator().AddColumn(&models.MaOrderNotify{}, "merchant_id")
	mysql.NewDB().Migrator().AddColumn(&models.MaRedemptionLog{}, "merchant_id")
	mysql.NewDB().Migrator().AddColumn(&models.MaRobot{}, "merchant_id")
	mysql.NewDB().Exec("update ma_order set merchant_id = id")
	mysql.NewDB().Exec("update ma_order_notify set merchant_id = id")
	mysql.NewDB().Exec("update ma_redemption_log set merchant_id = id")
	mysql.NewDB().Exec("update ma_robot set merchant_id = id")
	return nil
}
